﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Tank90
{
    /// <summary>
    ///     类Checkpoint。
    /// </summary>
    [Serializable]
    public class Checkpoint
    {
        public Checkpoint()
        {
        }

        #region Model

        private int? _adjustrandom;
        private int _checkid;
        private int? _firerandom;
        private int? _moverandom;

        /// <summary>
        /// </summary>
        public int CheckID
        {
            set { _checkid = value; }
            get { return _checkid; }
        }

        /// <summary>
        /// </summary>
        public int? FireRandom
        {
            set { _firerandom = value; }
            get { return _firerandom; }
        }

        /// <summary>
        /// </summary>
        public int? MoveRandom
        {
            set { _moverandom = value; }
            get { return _moverandom; }
        }

        /// <summary>
        /// </summary>
        public int? AdjustRandom
        {
            set { _adjustrandom = value; }
            get { return _adjustrandom; }
        }

        #endregion Model

        #region  Method

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public Checkpoint(int CheckID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select CheckID,FireRandom,MoveRandom,AdjustRandom ");
            strSql.Append(" FROM [Checkpoint] ");
            strSql.Append(" where CheckID=@CheckID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = CheckID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CheckID"] != null && ds.Tables[0].Rows[0]["CheckID"].ToString() != "")
                {
                    this.CheckID = int.Parse(ds.Tables[0].Rows[0]["CheckID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FireRandom"] != null && ds.Tables[0].Rows[0]["FireRandom"].ToString() != "")
                {
                    FireRandom = int.Parse(ds.Tables[0].Rows[0]["FireRandom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MoveRandom"] != null && ds.Tables[0].Rows[0]["MoveRandom"].ToString() != "")
                {
                    MoveRandom = int.Parse(ds.Tables[0].Rows[0]["MoveRandom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AdjustRandom"] != null &&
                    ds.Tables[0].Rows[0]["AdjustRandom"].ToString() != "")
                {
                    AdjustRandom = int.Parse(ds.Tables[0].Rows[0]["AdjustRandom"].ToString());
                }
            }
        }

        /// <summary>
        ///     得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CheckID", "Checkpoint");
        }

        /// <summary>
        ///     是否存在该记录
        /// </summary>
        public bool Exists(int CheckID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from [Checkpoint]");
            strSql.Append(" where CheckID=@CheckID ");

            SqlParameter[] parameters =
            {
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = CheckID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        ///     增加一条数据
        /// </summary>
        public void Add()
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into [Checkpoint] (");
            strSql.Append("CheckID,FireRandom,MoveRandom,AdjustRandom)");
            strSql.Append(" values (");
            strSql.Append("@CheckID,@FireRandom,@MoveRandom,@AdjustRandom)");
            SqlParameter[] parameters =
            {
                new SqlParameter("@CheckID", SqlDbType.Int, 4),
                new SqlParameter("@FireRandom", SqlDbType.Int, 4),
                new SqlParameter("@MoveRandom", SqlDbType.Int, 4),
                new SqlParameter("@AdjustRandom", SqlDbType.Int, 4)
            };
            parameters[0].Value = CheckID;
            parameters[1].Value = FireRandom;
            parameters[2].Value = MoveRandom;
            parameters[3].Value = AdjustRandom;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update()
        {
            var strSql = new StringBuilder();
            strSql.Append("update [Checkpoint] set ");
            strSql.Append("FireRandom=@FireRandom,");
            strSql.Append("MoveRandom=@MoveRandom,");
            strSql.Append("AdjustRandom=@AdjustRandom");
            strSql.Append(" where CheckID=@CheckID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@FireRandom", SqlDbType.Int, 4),
                new SqlParameter("@MoveRandom", SqlDbType.Int, 4),
                new SqlParameter("@AdjustRandom", SqlDbType.Int, 4),
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = FireRandom;
            parameters[1].Value = MoveRandom;
            parameters[2].Value = AdjustRandom;
            parameters[3].Value = CheckID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int CheckID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from [Checkpoint] ");
            strSql.Append(" where CheckID=@CheckID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = CheckID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public void GetModel(int CheckID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select CheckID,FireRandom,MoveRandom,AdjustRandom ");
            strSql.Append(" FROM [Checkpoint] ");
            strSql.Append(" where CheckID=@CheckID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = CheckID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CheckID"] != null && ds.Tables[0].Rows[0]["CheckID"].ToString() != "")
                {
                    this.CheckID = int.Parse(ds.Tables[0].Rows[0]["CheckID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FireRandom"] != null && ds.Tables[0].Rows[0]["FireRandom"].ToString() != "")
                {
                    FireRandom = int.Parse(ds.Tables[0].Rows[0]["FireRandom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MoveRandom"] != null && ds.Tables[0].Rows[0]["MoveRandom"].ToString() != "")
                {
                    MoveRandom = int.Parse(ds.Tables[0].Rows[0]["MoveRandom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AdjustRandom"] != null &&
                    ds.Tables[0].Rows[0]["AdjustRandom"].ToString() != "")
                {
                    AdjustRandom = int.Parse(ds.Tables[0].Rows[0]["AdjustRandom"].ToString());
                }
            }
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [Checkpoint] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}